package com.jianshuai.service.impl;

import com.jianshuai.domain.Menu;
import com.jianshuai.domain.Order;
import com.jianshuai.mapper.OrderMapper;
import com.jianshuai.service.IOrderService;
import com.jianshuai.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public AjaxResult addOrder(Order order) {
        //return orderMapper.addOrder(order);
        Long[] menuId = order.getMenuId();
        Long userId = order.getUserId();
        for (Long id : menuId){
            orderMapper.addOrder(userId,id);
        }
        return new AjaxResult();
    }


    @Override
    public AjaxResult addMenu(List<Menu> menus) {
        orderMapper.addMenu(menus);
        return new AjaxResult();
    }

    @Override
    public List<Order> getOrderList(Long userId) {
        List<Order> orders = new ArrayList<>();
        Order order = new Order();

        List<Menu> orderByUserId = orderMapper.getOrderByUserId(userId);

        order.setMenuList(orderByUserId);
        Order order1 = orderMapper.getorderByIdAndPrice(userId);
        order.setOrderId(order1.getOrderId());
        order.setTotalPrice(order1.getTotalPrice());
        orders.add(order);
        return orders;
    }

}
